package com.help.knowhelps.ui;


import com.help.knowhelps.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity{

	EditText et_phone,et_code;
	TextView submit;
	TextView tv_tips;
	Button bt_send;
	Boolean isfindback = false;//密码找回流程?  false时为手机注册流程
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.register_activity);
		initComponent();
	}
	private void initComponent()
	{
		try{
			isfindback = getIntent().getBooleanExtra("FindBackCode", false);
		}catch(Exception e )
		{
			e.printStackTrace();
		}
		et_phone = (EditText) findViewById(R.id.register_editText_phone);
		et_code = (EditText) findViewById(R.id.register_editText_code);
		tv_tips = (TextView) findViewById(R.id.register_TextView_tips);
		submit = (TextView) findViewById(R.id.register_TextView_submit);
		bt_send = (Button) findViewById(R.id.register_button_send);
		if(isfindback)
		{
			UiUtils.InitActionBar(this, "密码找回", UiUtils.ACTIONBAR_BUTTON_DEFAULT, 
					UiUtils.ACTIONBAR_BUTTON_DISAPPEAR ,clicklistener);
			submit.setText("确认");
		}else {
			UiUtils.InitActionBar(this, "手机注册", UiUtils.ACTIONBAR_BUTTON_DEFAULT, 
					UiUtils.ACTIONBAR_BUTTON_DISAPPEAR ,clicklistener);
		}
		submit.setOnClickListener(clicklistener);
		bt_send.setOnClickListener(clicklistener);
		et_phone.setOnFocusChangeListener(new OnFocusChangeListener()
		{
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus){
					if(et_phone.getText().toString().trim().length()<11){
						Toast.makeText(RegisterActivity.this, "请输入正确的11位手机号码", Toast.LENGTH_SHORT).show();
					}
				}
			}
			
		});
		
	}
		
//		protected void onCreate22(Bundle savedInstanceState) {
//			// TODO Auto-generated method stub
//			super.onCreate(savedInstanceState);
//			
//			setContentView(R.layout.user_register);
//			register_username=(EditText)findViewById(R.id.register_username);
//			register_passwd=(EditText)findViewById(R.id.register_passwd);
//			reregister_passwd=(EditText)findViewById(R.id.reregister_passwd);
//			register_submit=(Button)findViewById(R.id.register_submit);
//			register_username.setOnFocusChangeListener(new OnFocusChangeListener()
//			{
//				@Override
//				public void onFocusChange(View v, boolean hasFocus) {
//					// TODO Auto-generated method stub
//					if(!hasFocus){
//						if(register_username.getText().toString().trim().length()<4){
//							Toast.makeText(UserRegister.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT).show();
//						}
//					}
//				}
//				
//			});
//			register_passwd.setOnFocusChangeListener(new OnFocusChangeListener()
//			{
//
//				@Override
//				public void onFocusChange(View v, boolean hasFocus) {
//					// TODO Auto-generated method stub
//					if(!hasFocus){
//						if(register_passwd.getText().toString().trim().length()<6){
//							Toast.makeText(UserRegister.this, "密码不能小于8个字符", Toast.LENGTH_SHORT).show();
//						}
//					}
//				}
//			});
//			reregister_passwd.setOnFocusChangeListener(new OnFocusChangeListener()
//			{
//				@Override
//				public void onFocusChange(View v, boolean hasFocus) {
//					// TODO Auto-generated method stub
//					if(!hasFocus){
//						if(!reregister_passwd.getText().toString().trim().equals(register_passwd.getText().toString().trim())){
//							Toast.makeText(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show(); 
//						}
//					}
//				}
//			});
//			register_submit.setOnClickListener(new OnClickListener(){
//
//				@Override
//				public void onClick(View v) {
//					
//					if(!checkEdit()){
//						return;
//					}
//					// TODO Auto-generated method stub
//					String httpUrl="http://192.168.1.100:8080/web-test/register.jsp";
//					HttpPost httpRequest=new HttpPost(httpUrl);
//					List<NameValuePair> params=new ArrayList<NameValuePair>();
//					params.add(new BasicNameValuePair("username",register_username.getText().toString().trim()));
//					params.add(new BasicNameValuePair("password",register_passwd.getText().toString().trim()));
//					HttpEntity httpentity = null;
//					try {
//						httpentity = new UrlEncodedFormEntity(params,"utf8");
//					} catch (UnsupportedEncodingException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					httpRequest.setEntity(httpentity);
//					HttpClient httpclient=new DefaultHttpClient();
//					HttpResponse httpResponse = null;
//					try {
//						httpResponse = httpclient.execute(httpRequest);
//					} catch (ClientProtocolException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					if(httpResponse.getStatusLine().getStatusCode()==200)
//					{
//						String strResult = null;
//						try {
//							strResult = EntityUtils.toString(httpResponse.getEntity());
//						} catch (ParseException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						} catch (IOException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//						Toast.makeText(UserRegister.this, strResult, Toast.LENGTH_SHORT).show();
//					}
//					else
//					{
//						Toast.makeText(UserRegister.this, "请求错误", Toast.LENGTH_SHORT).show();
//					}
//					
//				}
//				
//			});
//		}
//		
//		private boolean checkEdit(){
//			if(register_username.getText().toString().trim().equals("")){
//				Toast.makeText(UserRegister.this,"用户名不能为空", Toast.LENGTH_SHORT).show();
//	}else if(register_passwd.getText().toString().trim().equals("")){
//	Toast.makeText(UserRegister.this, "密码不能为空", Toast.LENGTH_SHORT).show();
//}else if(!register_passwd.getText().toString().trim().equals(reregister_passwd.getText().toString().trim())){
//	Toast.makeText(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
//			}else{
//				return true;
//			}
//			return false;
//		}
//		
//	}
	
	private boolean checkEdit() {
		if (et_phone.getText().toString().trim().equals("")) {
			Toast.makeText(RegisterActivity.this,"手机号不能为空", Toast.LENGTH_SHORT).show();
		} else if (et_code.getText().toString().trim().equals("")) {
			Toast.makeText(RegisterActivity.this, "请输入当前手机收到的验证码", Toast.LENGTH_SHORT).show();
		} else {
			return true;
		}
		return false;
	}
	TimeCount time;
	private OnClickListener clicklistener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.actionbar_imageView_left:
				finish();
				break;
			case R.id.register_button_send:
				if(et_phone.getText().toString().trim().length()<11){
					Toast.makeText(RegisterActivity.this, "请输入正确的11位手机号码", Toast.LENGTH_SHORT).show();
				}else
				{
					time = new TimeCount(60000, 1000);
					time.start();
					//do... 生成验证码并发送
	//				(sendcomplete)回调:
					{
						tv_tips.setText("已发送验证码到:" + et_phone.getText().toString().trim());
						tv_tips.setVisibility(View.VISIBLE);
					}
						
				}
				break;
			case R.id.register_TextView_submit:
				if(!checkEdit())
					return;
				String phone = et_phone.getText().toString().trim();
				String code = et_code.getText().toString().trim();
				if(isfindback)
				{
					Intent it_findback = new Intent(RegisterActivity.this,FindBackActivity.class);
					it_findback.putExtra("phoneNum", phone);
					startActivity(it_findback);
					finish();
				}else //if
//				if(null == phone||phone.equals("")||null == code||code.equals(""))
//				{
//					Toast.makeText(RegisterActivity.this, "账号或密码输入有误,请重新输入!", Toast.LENGTH_SHORT).show();
//				}else	
//				if(code.equals("验证码**********"))
				{
					Intent it_registerinfo = new Intent(RegisterActivity.this,RegisterInfoActivity.class);
					it_registerinfo.putExtra("phoneNum", phone);
					startActivity(it_registerinfo);
					finish();
				}
				break;
			default:
				break;
			}
		}
	};
	
	class TimeCount extends CountDownTimer {

		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			bt_send.setBackgroundColor(Color.parseColor("#B6B6D8"));
			bt_send.setClickable(false);
			bt_send.setText(millisUntilFinished / 1000 + "秒后可重新发送");
		}

		@Override
		public void onFinish() {
			bt_send.setText("重新获取验证码");
			bt_send.setClickable(true);
			bt_send.setBackgroundColor(Color.parseColor("#4EB84A"));

		}
	}
}